//
//  KLPhotoBroswerCell.m
//  HealthManage
//
//  Created by 陈华 on 2017/4/10.
//  Copyright © 2017年 kinglian. All rights reserved.
//

#import "KLPhotoBroswerCell.h"
#import "GPMessageItem.h"

#import <UIImageView+WebCache.h>

@interface KLPhotoBroswerCell ()

@property(nonatomic,weak) UIImageView *imageView;

@property(nonatomic,weak) UIActivityIndicatorView *indicatorView;

@end

@implementation KLPhotoBroswerCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        //设置UI
        [self setupUI];
        
    }
    return self;
}

#pragma mark - 设置UI
/**设置UI*/
- (void)setupUI
{
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [imageView addGestureRecognizer:tap];
    [self.contentView addSubview:imageView];
    self.imageView = imageView;
    
    
    UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    indicatorView.hidesWhenStopped = YES;
    [self.contentView addSubview:indicatorView];
    self.indicatorView = indicatorView;
}

- (void)layoutSubviews
{
    self.imageView.frame = self.contentView.frame;
    self.indicatorView.center = self.contentView.center;
}


#pragma mark - setter方法重写
- (void)setMessage:(GPMessageItem *)message
{
    _message = message;
    
//    获取图片地址
//    NSString *name = imageContent[kChatImageMessageLocalKey];
//    NSString *urlStr = [[[KLPathManager shareManager] currentDirectiory:KLDirectoryTypeCurrentChatImage] stringByAppendingPathComponent:name];
//    if (name.length == 0) {
//        urlStr = imageContent[kChatImageMessageBigKey];
//        //获取缩略图
//        UIImage *thumal = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imageContent[kChatImageMessageThumailKey]];
//        
//        //设置图片
//        [self.indicatorView startAnimating];
//        self.indicatorView.hidden = NO;
//        [self.imageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:thumal completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//            [self.indicatorView stopAnimating];
//            self.indicatorView.hidden = YES;
//        }];
//    }else{
//        [self.imageView sd_setImageWithURL:[NSURL fileURLWithPath:urlStr]];
//    }
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:message.fileURLPath] placeholderImage:message.image];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
}

#pragma mark - 事件监听
- (void)tap
{
    if (self.option) {
        self.option();
    }
}

@end
